/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ventanas;

import Gestores.DtoObjects.BitacoraDTO;
import Gestores.Entidades.EntidadBitacora;
import Gestores.Entidades.EntidadCuentaPago;
import Gestores.Entidades.EntidadDetalleEgreso;
import Gestores.Entidades.EntidadPermiso;
import Negocios.DetalleEgresoNegocio;
import Negocios.GestionCuentaPagoNegocio;
import Util.Acciones;
import static java.awt.image.ImageObserver.WIDTH;

import java.sql.Date;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

/**
 *
 * @author Lion
 */
public class GestionDetalleEgreso extends javax.swing.JFrame {

    private DetalleEgresoNegocio negocio;
    private GestionCuentaPagoNegocio cuenta;
    private DefaultComboBoxModel model;
    private EntidadPermiso permiso;
    ArrayList<Object> l;
    private EntidadBitacora b;
    private BitacoraDTO dto;
    /**
     * Creates new form GestionDetalleEgreso
     */
    public GestionDetalleEgreso() {
        initComponents();
        this.cuenta = new GestionCuentaPagoNegocio();
        this.dto= new BitacoraDTO();
        try {
            model = new DefaultComboBoxModel();
            model.addElement("Seleccionar Cuenta");
             l = this.cuenta.listarT();
            for (int i = 0; i < l.size(); i++) {
                EntidadCuentaPago cp = (EntidadCuentaPago) l.get(i);
                model.addElement(cp.getDescripcion());
            }
            cmb_cuenta.setModel(model);
            //model.addElement();
        } catch (Exception ex) {
            Logger.getLogger(GestionDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        this.negocio = new DetalleEgresoNegocio();
        // this.cmb_cuenta.setModel(null);
    }
    
    public GestionDetalleEgreso(EntidadPermiso permiso) {
        this.permiso = permiso;
        initComponents();
        
        this.cuenta = new GestionCuentaPagoNegocio();
        try {
            model = new DefaultComboBoxModel();
            model.addElement("Seleccionar Cuenta");
            ArrayList<Object> l = this.cuenta.listarT();
            for (int i = 0; i < l.size(); i++) {
                EntidadCuentaPago cp = (EntidadCuentaPago) l.get(i);
                model.addElement(cp.getDescripcion());
            }
            cmb_cuenta.setModel(model);
            //model.addElement();
        } catch (Exception ex) {
            Logger.getLogger(GestionDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        this.negocio = new DetalleEgresoNegocio();
        // this.cmb_cuenta.setModel(null);
        setComponentsDisabled();
        configurarFrame();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txt_codDetalle = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txt_usuario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cmb_cuenta = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txt_monto = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        cmb_date = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txt_mensaje = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Colegio Economistas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Gestion Cuentas Pagos"));
        jPanel1.setLayout(new java.awt.GridLayout(5, 2));

        jLabel6.setText("Cod Detalle");
        jPanel1.add(jLabel6);

        txt_codDetalle.setEditable(false);
        txt_codDetalle.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel1.add(txt_codDetalle);

        jLabel4.setText("Usuario");
        jPanel1.add(jLabel4);

        txt_usuario.setEditable(false);
        txt_usuario.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txt_usuario.setText("1");
        jPanel1.add(txt_usuario);

        jLabel1.setText("Cuenta");
        jPanel1.add(jLabel1);

        cmb_cuenta.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(cmb_cuenta);

        jLabel2.setText("Monto");
        jPanel1.add(jLabel2);

        txt_monto.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        txt_monto.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel1.add(txt_monto);

        jLabel3.setText("Fecha");
        jPanel1.add(jLabel3);
        jPanel1.add(cmb_date);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButton1.setText("Nuevo");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        jButton2.setText("Pagar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        jButton3.setText("Buscar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton3);

        jButton4.setText("Eliminar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton4);

        jLabel5.setText("Mensaje");

        txt_mensaje.setColumns(20);
        txt_mensaje.setRows(5);
        jScrollPane1.setViewportView(txt_mensaje);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane1)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        model.setSelectedItem("Seleccionar Cuenta");
        this.txt_monto.setText("");
        this.txt_mensaje.setText("");
        this.cmb_date.setDate(null);
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (!this.txt_usuario.getText().isEmpty()) {
            if (!this.txt_monto.getText().isEmpty()) {
                if (this.cmb_date.getDate() != null) {
                    if (this.cmb_cuenta.getSelectedIndex() != 0) {
                        try {
                            //insertar(int u, float monto, Date f, Object cuenta)
                            negocio.insertar(Integer.valueOf(txt_usuario.getText()),
                                    Float.valueOf(this.txt_monto.getText()), this.cmb_date.getDate(),
                                    this.cmb_cuenta.getSelectedItem());
                            this.txt_mensaje.setText("Transaccion Realizada Con Exito..!!");
                             this.b = new EntidadBitacora("pago Cuenta  " + (String)this.cmb_cuenta.getSelectedItem(), Principal.Usuario,
                                Principal.CajaChica, Acciones.Insert);
                        try {
                            this.dto.insertar(b);
                        } catch (Exception ex) {
                            Logger.getLogger(BuscarDetallePago.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        } catch (Exception ex) {
                            this.txt_mensaje.setText(ex.getMessage());
                            Logger.getLogger(GestionDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } else {
                        this.txt_mensaje.setText("Error, debe selecionar una Cuenta");
                    }
                    
                } else {
                    this.txt_mensaje.setText("Error, Ingresar La Fecha");
                }
            } else {
                this.txt_mensaje.setText("Error, debe Ingresar el Monto");
            }
        } else {
            this.txt_mensaje.setText("Error, debe ingresar el codigo de usuario");
        }
        // this.negocio.insertar();
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        BuscarDetalleEgreso ventana = new BuscarDetalleEgreso();
        ventana.enlazar(this);
        ventana.setVisible(true);
        ventana.requestFocus();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if(this.txt_codDetalle.getText()!=null && this.txt_codDetalle.getText()!=""){
            if(this.cmb_cuenta.getSelectedIndex()!=0){
                EntidadCuentaPago p=(EntidadCuentaPago) this.l.get(this.cmb_cuenta.getSelectedIndex()-1);
                try {
                    this.negocio.eliminacionLogica(Integer.valueOf(this.txt_codDetalle.getText()),
                            p.getId(), Integer.valueOf(this.txt_usuario.getText()));
                    this.txt_mensaje.setText("Eliminado");
                     this.b = new EntidadBitacora("eliminacion pago cuenta" + (String)this.cmb_cuenta.getSelectedItem(), 
                     Principal.Usuario,
                                Principal.CajaChica, Acciones.Drop);
                        try {
                            this.dto.insertar(b);
                        } catch (Exception ex) {
                            Logger.getLogger(BuscarDetallePago.class.getName()).log(Level.SEVERE, null, ex);
                        }
                } catch (Exception ex) {
                    Logger.getLogger(GestionDetalleEgreso.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GestionDetalleEgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GestionDetalleEgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GestionDetalleEgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GestionDetalleEgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GestionDetalleEgreso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmb_cuenta;
    private com.toedter.calendar.JDateChooser cmb_date;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txt_codDetalle;
    private javax.swing.JTextArea txt_mensaje;
    private javax.swing.JFormattedTextField txt_monto;
    private javax.swing.JTextField txt_usuario;
    // End of variables declaration//GEN-END:variables

    private void setComponentsDisabled() {
        this.cmb_cuenta.setEnabled(false);
        this.cmb_date.setEnabled(false);
        this.jButton2.setEnabled(false);
        this.jButton3.setEnabled(false);
        this.jButton4.setEnabled(false);
        this.txt_monto.setEnabled(false);
        this.txt_usuario.setEnabled(false);
    }
    
    private void configurarFrame() {
        if (this.permiso.isAnadir()) {
            this.jButton2.setEnabled(true);
            this.txt_usuario.setEnabled(true);
            this.txt_monto.setEnabled(true);
            this.cmb_cuenta.setEnabled(true);
            this.cmb_date.setEnabled(true);
        }
        if (this.permiso.isBuscar()) {
            this.jButton3.setEnabled(true);
        }
        if (this.permiso.isEliminar()) {
            this.jButton4.setEnabled(true);
        }
        
    }
    
    public void cargarDatos(Object object) {
        EntidadDetalleEgreso detalle = (EntidadDetalleEgreso) object;
        this.txt_usuario.setText(String.valueOf(detalle.getId_usuario()));
        this.txt_monto.setText(String.valueOf(detalle.getMonto()));
        this.cmb_cuenta.setSelectedItem(detalle.getCuenta());
        this.cmb_date.setDate(detalle.getFecha());
        this.txt_codDetalle.setText(String.valueOf(detalle.getId()));
    }
}
